/*
 * @Author: hh
 * @Date: 2025-09-02 15:10:42
 * @LastEditors: hh
 * @LastEditTime: 2025-09-02 15:34:17
 * @Description:
 */
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { ApiProperty, ApiHideProperty } from '@nestjs/swagger';
import { User } from '../../user/entities/user.entity';
import { OrderItem } from './order-item.entity';

export enum OrderStatus {
  PENDING = 'pending',
  PAID = 'paid',
  SHIPPED = 'shipped',
  DELIVERED = 'delivered',
  CANCELLED = 'cancelled',
}

@Entity('orders')
export class Order {
  @PrimaryGeneratedColumn('uuid')
  @ApiProperty({ description: '订单ID' })
  id: string;

  @Column({ unique: true })
  @ApiProperty({ description: '订单编号' })
  order_number: string;

  @ApiProperty({ description: '下单用户', type: () => User })
  @ManyToOne(() => User, (user) => user.orders)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column()
  @ApiProperty({ description: '用户ID' })
  user_id: string;

  @Column('decimal', { precision: 10, scale: 2 })
  @ApiProperty({ description: '订单总金额' })
  total_amount: number;

  @Column({
    type: 'enum',
    enum: OrderStatus,
    default: OrderStatus.PENDING,
  })
  @ApiProperty({ description: '订单状态', enum: OrderStatus })
  status: OrderStatus;

  @Column({ nullable: true })
  @ApiProperty({ description: '收货地址' })
  shipping_address: string;

  @Column({ nullable: true })
  @ApiProperty({ description: '联系电话' })
  phone: string;

  @Column({ nullable: true })
  @ApiProperty({ description: '备注' })
  note: string;

  @ApiHideProperty()
  @OneToMany(() => OrderItem, (orderItem) => orderItem.order, { cascade: true })
  items: OrderItem[];

  @CreateDateColumn()
  @ApiProperty({ description: '创建时间' })
  created_at: Date;

  @UpdateDateColumn()
  @ApiProperty({ description: '更新时间' })
  updated_at: Date;
}
